#ifndef CONNECTION_H
#define CONNECTION_H

#include <QtSql>
#include <QDate>
#include <QTime>
#include <QString>
#include <QMessageBox>

static bool createConnection()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(":memory:");
        if (!db.open()) {
            QMessageBox::critical(nullptr, QObject::tr("Cannot open database"),
            QObject::tr("Unable to establish a database connection.\n"
            "This example needs SQLite support. Please read "
            "the Qt SQL driver documentation for information how "
            "to build it.\n\n"
            "Click Cancel to exit."), QMessageBox::Cancel);
        return false;
        }

        QSqlQuery query;
        QString mydt = QDate::currentDate().toString("yyyy-MM-dd");
        QString mytime= QTime::currentTime().toString("hh:mm:ss");

        query.exec("create table student (id int primary key, "
            "firstname varchar(20), lastname varchar(20), dt varchar(20), clock varchar(20))");

        query.prepare("insert into student(id,firstname,lastname,dt,clock) values(?,?,?,?,?)");
        query.addBindValue(1);
        query.addBindValue("Danny");
        query.addBindValue("Young");
        query.addBindValue(mydt);
        query.addBindValue(mytime);
        query.exec();

        query.prepare("insert into student(id,firstname,lastname,dt,clock) values(?,?,?,?,?)");
        query.addBindValue(2);
        QString fname="chen";
        QString lname="hong";
        query.addBindValue(fname);
        query.addBindValue(lname);
        query.addBindValue(mydt);
        query.addBindValue(mytime);
        query.exec();


    return true;
}

#endif // CONNECTION_H
